/*******************************************************************************
 *                                                                              *
 * Basic JavaScript BN library - subset useful for RSA encryption.              *
 * http://www-cs-students.stanford.edu/~tjw/jsbn/                               *
 * Copyright (c) 2005  Tom Wu                                                   *
 * All Rights Reserved.                                                         *
 * See "LICENSE" for details:                                                   *
 * http://www-cs-students.stanford.edu/~tjw/jsbn/LICENSE                        *
 *                                                                              *
 *******************************************************************************/
// Copyright (c) 2005  Tom Wu
// All Rights Reserved.
// See "LICENSE" for details.
let navigator_appName;
const isNode = false
let nav
if (!isNode) {
  nav = navigator.userAgent.toString().toLowerCase();
  navigator_appName = navigator.appName;
} else {
  nav = "chrome"; // Node.js uses Chrome's V8 engine
  navigator_appName = "Netscape"; // Firefox, Chrome and Safari returns "Netscape", so Node.js should also
}
// Browser test to speedup performance critical functions
export const browser = {};

if (nav.indexOf("chrome") != -1 && nav.indexOf("chromium") == -1) browser.chrome = 1;
else browser.chrome = 0;
if (nav.indexOf("chromium") != -1) browser.chromium = 1;
else browser.chromium = 0;
if (nav.indexOf("safari") != -1 && nav.indexOf("chrome") == -1 && nav.indexOf("chromium") == -1) browser.safari = 1;
else browser.safari = 0;
if (nav.indexOf("firefox") != -1) browser.firefox = 1;
else browser.firefox = 0;
if (nav.indexOf("firefox/17") != -1) browser.firefox17 = 1;
else browser.firefox17 = 0;
if (nav.indexOf("firefox/15") != -1) browser.firefox15 = 1;
else browser.firefox15 = 0;
if (nav.indexOf("firefox/3") != -1) browser.firefox3 = 1;
else browser.firefox3 = 0;
if (nav.indexOf("opera") != -1) browser.opera = 1;
else browser.opera = 0;
if (nav.indexOf("msie 10") != -1) browser.msie10 = 1;
else browser.msie10 = 0;
if (nav.indexOf("msie 9") != -1) browser.msie9 = 1;
else browser.msie9 = 0;
if (nav.indexOf("msie 8") != -1) browser.msie8 = 1;
else browser.msie8 = 0;
if (nav.indexOf("msie 7") != -1) browser.msie7 = 1;
else browser.msie7 = 0;
if (nav.indexOf("msie ") != -1) browser.msie = 1;
else browser.msie = 0;

// Basic JavaScript BN library - subset useful for RSA encryption.
// Bits per digit
let dbits;
// JavaScript engine analysis
const canary = 0xdeadbeefcafe;
let j_lm = ((canary & 0xffffff) == 0xefcafe);

// am: Compute w_j += (x*this_i), propagate carries,
// c is initial carry, returns final carry.
// c < 3*dvalue, x < 2*dvalue, this_i < dvalue
// We need to select the fastest one that works in this environment.
// am1: use a single mult and divide to get the high bits,
// max digit bits should be 26 because
// max internal value = 2*dvalue^2-2*dvalue (< 2^53)
function am1(i, x, w, j, c, n) {
  while (--n >= 0) {
    const v = x * this[i++] + w[j] + c;
    c = Math.floor(v / 0x4000000);
    w[j++] = v & 0x3ffffff;
  }
  return c;
}
// am2 avoids a big mult-and-extract completely.
// Max digit bits should be <= 30 because we do bitwise ops
// on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
function am2(i, x, w, j, c, n) {
  const xl = x & 0x7fff,
    xh = x >> 15;
  while (--n >= 0) {
    let l = this[i] & 0x7fff;
    const h = this[i++] >> 15;
    const m = xh * l + h * xl;
    l = xl * l + ((m & 0x7fff) << 15) + w[j] + (c & 0x3fffffff);
    c = (l >>> 30) + (m >>> 15) + xh * h + (c >>> 30);
    w[j++] = l & 0x3fffffff;
  }
  return c;
}
// Alternately, set max digit bits to 28 since some
// browsers slow down when dealing with 32-bit numbers.
function am3(i, x, w, j, c, n) {
  const xl = x & 0x3fff,
    xh = x >> 14;
  while (--n >= 0) {
    let l = this[i] & 0x3fff;
    const h = this[i++] >> 14;
    const m = xh * l + h * xl;
    l = xl * l + ((m & 0x3fff) << 14) + w[j] + c;
    c = (l >> 28) + (m >> 14) + xh * h;
    w[j++] = l & 0xfffffff;
  }
  return c;
}

const here = {}
here.DB = dbits;
here.DM = ((1 << dbits) - 1);
here.DV = (1 << dbits);
const BI_FP = 52;
here.FV = Math.pow(2, BI_FP);
here.F1 = BI_FP - dbits;
here.F2 = 2 * dbits - BI_FP;

// Digit conversions
const BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
const BI_RC = new Array();
let rr, vv;
rr = "0".charCodeAt(0);
for (vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
rr = "a".charCodeAt(0);
for (vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
rr = "A".charCodeAt(0);
for (vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;

function int2char(n) {
  return BI_RM.charAt(n);
}
function intAt(s, i) {
  const c = BI_RC[s.charCodeAt(i)];
  return (c == null) ? -1 : c;
}

function op_and(x, y) {
  return x & y;
}
function op_or(x, y) {
  return x | y;
}
function op_xor(x, y) {
  return x ^ y;
}
function op_andnot(x, y) {
  return x & ~y;
}
// returns bit length of the integer x
function nbits(x) {
  let r = 1, t;
  if ((t = x >>> 16) != 0) {
    x = t;
    r += 16;
  }
  if ((t = x >> 8) != 0) {
    x = t;
    r += 8;
  }
  if ((t = x >> 4) != 0) {
    x = t;
    r += 4;
  }
  if ((t = x >> 2) != 0) {
    x = t;
    r += 2;
  }
  if ((t = x >> 1) != 0) {
    x = t;
    r += 1;
  }
  return r;
}
// return index of lowest 1-bit in x, x < 2^31
function lbit(x) {
  if (x == 0) return -1;
  let r = 0;
  if ((x & 0xffff) == 0) {
    x >>= 16;
    r += 16;
  }
  if ((x & 0xff) == 0) {
    x >>= 8;
    r += 8;
  }
  if ((x & 0xf) == 0) {
    x >>= 4;
    r += 4;
  }
  if ((x & 3) == 0) {
    x >>= 2;
    r += 2;
  }
  if ((x & 1) == 0) ++r;
  return r;
}
// return number of 1 bits in x
function cbit(x) {
  let r = 0;
  while (x != 0)
  {
    x &= x - 1;
    ++r;
  }
  return r;
}
const lowprimes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997];
const lplim = (1 << 26) / lowprimes[lowprimes.length - 1];

export class BigInteger {
  static ZERO
  static ONE

  constructor(a, b, c) {
    if (a != null)
      if ("number" == typeof a && "undefined" == typeof (b)) this.bnpFromInt(a); // faster conversion
      else if ("number" == typeof a) this.bnpFromNumber(a, b, c);
      else if (b == null && "string" != typeof a) this.bnpFromString(a, 256);
      else this.bnpFromString(a, b);
  }
  bnpCopyTo(r) {
    for (let i = this.t - 1; i >= 0; --i) r[i] = this[i];
    r.t = this.t;
    r.s = this.s;
    return this
  }
  // set from integer value x, -DV <= x < DV
  bnpFromInt(x) {
    this.t = 1;
    this.s = (x < 0) ? -1 : 0;
    if (x > 0) this[0] = x;
    else if (x < -1) this[0] = x + here.DV;
    else this.t = 0;
    return this
  }
  // (protected) clamp off excess high words
  bnpClamp() {
    const c = this.s & here.DM;
    while (this.t > 0 && this[this.t - 1] == c) --this.t;
  }
  // (public) return string representation in given radix
  bnToString(b) {
    if (this.s < 0) return "-" + this.bnNegate().bnToString(b);
    let k;
    if (b == 16) k = 4;
    else if (b == 8) k = 3;
    else if (b == 2) k = 1;
    else if (b == 32) k = 5;
    else if (b == 4) k = 2;
    else return this.toRadix(b);
    let km = (1 << k) - 1,
      d, m = false,
      r = "",
      i = this.t;
    let p = here.DB - (i * here.DB) % k;
    if (i-- > 0) {
      if (p < this.DB && (d = this[i] >> p) > 0) {
        m = true;
        r = int2char(d);
      }
      while (i >= 0) {
        if (p < k) {
          d = (this[i] & ((1 << p) - 1)) << (k - p);
          d |= this[--i] >> (p += here.DB - k);
        } else {
          d = (this[i] >> (p -= k)) & km;
          if (p <= 0)
          {
            p += here.DB;
            --i;
          }
        }
        if (d > 0) m = true;
        if (m) r += int2char(d);
      }
    }
    return m ? r : "0";
  }
  bnNegate() {
    const r = new BigInteger()
    BigInteger.ZERO.bnpSubTo(this, r);
    return r;
  }
  bnAbs() {
    return (this.s < 0) ? this.bnNegate() : this;
  }
  // (public) return + if this > a, - if this < a, 0 if equal
  bnCompareTo(a) {
    let r = this.s - a.s;
    if (r != 0) return r;
    let i = this.t;
    r = i - a.t;
    if (r != 0) return (this.s < 0) ? -r : r;
    while (--i >= 0)
      if ((r = this[i] - a[i]) != 0) return r;
    return 0;
  }
  // (public) return the number of bits in "this"
  bnBitLength() {
    if (this.t <= 0) return 0;
    return here.DB * (this.t - 1) + nbits(this[this.t - 1] ^ (this.s & here.DM));
  }
  // (protected) r = this << n*DB
  bnpDLShiftTo(n, r) {
    let i;
    for (i = this.t - 1; i >= 0; --i) r[i + n] = this[i];
    for (i = n - 1; i >= 0; --i) r[i] = 0;
    r.t = this.t + n;
    r.s = this.s;
  }
  // (protected) r = this >> n*DB
  bnpDRShiftTo(n, r) {
    for (let i = n; i < this.t; ++i) r[i - n] = this[i];
    r.t = Math.max(this.t - n, 0);
    r.s = this.s;
  }
  // r = this << n
  bnpLShiftTo(n, r) {
    const bs = n % here.DB;
    const cbs = here.DB - bs;
    const bm = (1 << cbs) - 1;
    const ds = Math.floor(n / here.DB)
    let c = (this.s << bs) & here.DM
    let i;
    for (i = this.t - 1; i >= 0; --i) {
      r[i + ds + 1] = (this[i] >> cbs) | c;
      c = (this[i] & bm) << bs;
    }
    for (i = ds - 1; i >= 0; --i) r[i] = 0;
    r[ds] = c;
    r.t = this.t + ds + 1;
    r.s = this.s;
    r.bnpClamp();
    return r
  }
  bnpRShiftTo(n, r) {
    r.s = this.s;
    const ds = Math.floor(n / here.DB);
    if (ds >= this.t) {
      r.t = 0;
      return r;
    }
    const bs = n % here.DB;
    const cbs = here.DB - bs;
    const bm = (1 << bs) - 1;
    r[0] = this[ds] >> bs;
    for (let i = ds + 1; i < this.t; ++i) {
      r[i - ds - 1] |= (this[i] & bm) << cbs;
      r[i - ds] = this[i] >> bs;
    }
    if (bs > 0) r[this.t - ds - 1] |= (this.s & bm) << cbs;
    r.t = this.t - ds;
    r.bnpClamp();
    return r
  }
  // r = this - a
  bnpSubTo(a, r) {
    let i = 0,
      c = 0,
      m = Math.min(a.t, this.t);
    while (i < m) {
      c += this[i] - a[i];
      r[i++] = c & here.DM;
      c >>= here.DB;
    }
    if (a.t < this.t) {
      c -= a.s;
      while (i < this.t) {
        c += this[i];
        r[i++] = c & here.DM;
        c >>= here.DB;
      }
      c += this.s;
    }
    else {
      c += this.s;
      while (i < a.t)
      {
        c -= a[i];
        r[i++] = c & here.DM;
        c >>= here.DB;
      }
      c -= a.s;
    }
    r.s = (c < 0) ? -1 : 0;
    if (c < -1) r[i++] = here.DV + c;
    else if (c > 0) r[i++] = c;
    r.t = i;
    r.bnpClamp();
    return r
  }
  // r = this * a, r != this,a (HAC 14.12)
  // "this" should be the larger one if appropriate.
  bnpMultiplyTo(a, r) {
    const x = this.bnAbs(),
      y = a.bnAbs();
    let i = x.t;
    r.t = i + y.t;
    while (--i >= 0) r[i] = 0;
    for (i = 0; i < y.t; ++i) r[i + x.t] = x.am(0, y[i], r, i, 0, x.t);
    r.s = 0;
    r.bnpClamp();
    if (this.s != a.s) BigInteger.ZERO.bnpSubTo(r, r);
  }
  // r = this^2, r != this (HAC 14.16)
  bnpSquareTo(r) {
    const x = this.bnAbs();
    let i = r.t = 2 * x.t;
    while (--i >= 0) r[i] = 0;
    for (i = 0; i < x.t - 1; ++i) {
      const c = x.am(i, x[i], r, 2 * i, 0, 1);
      if ((r[i + x.t] += x.am(i + 1, 2 * x[i], r, 2 * i + 1, c, x.t - i - 1)) >= here.DV) {
        r[i + x.t] -= here.DV;
        r[i + x.t + 1] = 1;
      }
    }
    if (r.t > 0) r[r.t - 1] += x.am(i, x[i], r, 2 * i, 0, 1);
    r.s = 0;
    r.clamp();
  }
  // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
  // r != q, this != m.  q or r may be null.
  bnpDivRemTo(m, q, r) {
    const pm = m.bnAbs();
    if (pm.t <= 0) return;
    let pt = this.bnAbs();
    if (pt.t < pm.t) {
      if (q != null) q.bnpFromInt(0);
      if (r != null) this.bnpCopyTo(r);
      return;
    }
    if (r == null) r = new BigInteger()
    const y = new BigInteger(),
      ts = this.s,
      ms = m.s;
    const nsh = here.DB - nbits(pm[pm.t - 1]); // normalize modulus
    if (nsh > 0) {
      pm.bnpLShiftTo(nsh, y);
      pt.bnpLShiftTo(nsh, r);
    } else {
      pm.bnpCopyTo(y);
      pt.bnpCopyTo(r);
    }
    const ys = y.t;
    const y0 = y[ys - 1];
    if (y0 == 0) return;
    const yt = y0 * (1 << here.F1) + ((ys > 1) ? y[ys - 2] >> here.F2 : 0);
    const d1 = here.FV / yt,
      d2 = (1 << here.F1) / yt,
      e = 1 << here.F2;
    let i = r.t,
      j = i - ys,
      t = (q == null) ? new BigInteger() : q;
    y.bnpDLShiftTo(j, t);
    if (r.bnCompareTo(t) >= 0)
    {
      r[r.t++] = 1;
      r.bnpSubTo(t, r);
    }
    BigInteger.ONE.bnpDLShiftTo(ys, t);
    t.bnpSubTo(y, y); // "negative" y so we can replace sub with am later
    while (y.t < ys) y[y.t++] = 0;
    while (--j >= 0) {
      // Estimate quotient digit
      let qd = (r[--i] == y0) ? here.DM : Math.floor(r[i] * d1 + (r[i - 1] + e) * d2);
      if ((r[i] += y.am(0, qd, r, j, 0, ys)) < qd) {
        // Try it out
        y.bnpDLShiftTo(j, t);
        r.bnpSubTo(t, r);
        while (r[i] < --qd) r.bnpSubTo(t, r);
      }
    }
    if (q != null) {
      r.bnpDRShiftTo(ys, q);
      if (ts != ms) BigInteger.ZERO.bnpSubTo(q, q);
    }
    r.t = ys;
    r.bnpClamp();
    if (nsh > 0) r.bnpRShiftTo(nsh, r); // Denormalize remainder
    if (ts < 0) BigInteger.ZERO.bnpSubTo(r, r);
  }
  // (public) this mod a
  bnMod(a) {
    const r = new BigInteger()
    this.bnAbs().bnpDivRemTo(a, null, r);
    if (this.s < 0 && r.bnCompareTo(BigInteger.ZERO) > 0) a.bnpSubTo(r, r);
    return r;
  }
  // (protected) return "-1/this % 2^DB"; useful for Mont. reduction
  // justification:
  //         xy == 1 (mod m)
  //         xy =  1+km
  //   xy(2-xy) = (1+km)(1-km)
  // x[y(2-xy)] = 1-k^2m^2
  // x[y(2-xy)] == 1 (mod m^2)
  // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
  // should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
  // JS multiply "overflows" differently from C/C++, so care is needed here.
  bnpInvDigit() {
    if (this.t < 1) return 0;
    const x = this[0];
    if ((x & 1) == 0) return 0;
    let y = x & 3; // y == 1/x mod 2^2
    y = (y * (2 - (x & 0xf) * y)) & 0xf; // y == 1/x mod 2^4
    y = (y * (2 - (x & 0xff) * y)) & 0xff; // y == 1/x mod 2^8
    y = (y * (2 - (((x & 0xffff) * y) & 0xffff))) & 0xffff; // y == 1/x mod 2^16
    // last step - calculate inverse mod DV directly;
    // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
    y = (y * (2 - x * y % here.DV)) % here.DV; // y == 1/x mod 2^dbits
    // we really want the negative inverse, and -DV < y < DV
    return (y > 0) ? here.DV - y : -y;
  }


  // (protected) true iff this is even
  bnpIsEven() {
    return ((this.t > 0) ? (this[0] & 1) : this.s) == 0;
  }
  // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
  bnpExp(e, z) {
    if (e > 0xffffffff || e < 1) return BigInteger.ONE;
    let r = new BigInteger(),
      r2 = new BigInteger(),
      g = z.convert(this),
      i = nbits(e) - 1;
    g.bnpCopyTo(r);
    while (--i >= 0) {
      z.sqrTo(r, r2);
      if ((e & (1 << i)) > 0) z.mulTo(r2, g, r);
      else {
        const t = r;
        r = r2;
        r2 = t;
      }
    }
    return z.revert(r);
  }
  // (public) this^e % m, 0 <= e < 2^32
  bnModPowInt(e, m) {
    let z;
    if (e < 256 || m.bnpIsEven()) z = new Classic(m);
    else z = new Montgomery(m);
    return this.bnpExp(e, z);
  }
  bnClone() {
    const r = new BigInteger()
    this.bnpCopyTo(r)
    return r
  }

  // return value as integer
  bnIntValue() {
    if (this.s < 0) {
      if (this.t == 1) return this[0] - here.DV;
      else if (this.t == 0) return -1;
    }
    else if (this.t == 1) return this[0];
    else if (this.t == 0) return 0;
    // assumes 16 < DB < 32
    return ((this[1] & ((1 << (32 - here.DB)) - 1)) << here.DB) | this[0];
  }
  // return value as byte
  bnByteValue() {
    return (this.t == 0) ? this.s : (this[0] << 24) >> 24;
  }
  // return value as short (assumes DB>=16)
  bnShortValue() {
    return (this.t == 0) ? this.s : (this[0] << 16) >> 16;
  }
  // return x s.t. r^x < DV
  bnpChunkSize(r) {
    return Math.floor(Math.LN2 * here.DB / Math.log(r));
  }
  // 0 if this == 0, 1 if this > 0
  bnSigNum() {
    if (this.s < 0) return -1;
    else if (this.t <= 0 || (this.t == 1 && this[0] <= 0)) return 0;
    else return 1;
  }
  // convert to radix string
  bnpToRadix(b) {
    if (b == null) b = 10;
    if (this.bnSigNum() == 0 || b < 2 || b > 36) return "0";
    const cs = this.bnpChunkSize(b);
    const a = Math.pow(b, cs);
    const d = nbv(a)
    const y = new BigInteger()
    const z = new BigInteger()
    let r = "";
    this.bnpDivRemTo(d, y, z);
    while (y.bnSigNum() > 0) {
      r = (a + z.bnIntValue()).toString(b).substring(1) + r;
      y.bnpDivRemTo(d, y, z);
    }
    return z.bnIntValue().toString(b) + r;
  }
  bnpFromRadix(s, b) {
    this.bnpFromInt(0);
    if (b == null) b = 10;
    const cs = this.bnpChunkSize(b);
    let d = Math.pow(b, cs),
      mi = false,
      j = 0,
      w = 0;
    for (let i = 0; i < s.length; ++i) {
      const x = intAt(s, i);
      if (x < 0) {
        if (s.charAt(i) == "-" && this.bnSigNum() == 0) mi = true;
        continue;
      }
      w = b * w + x;
      if (++j >= cs) {
        this.bnpDMultiply(d);
        this.bnpDAddOffset(w, 0);
        j = 0;
        w = 0;
      }
    }
    if (j > 0) {
      this.bnpDMultiply(Math.pow(b, j));
      this.bnpDAddOffset(w, 0);
    }
    if (mi) BigInteger.ZERO.bnpSubTo(this, this);
  }
  // alternate constructor
  bnpFromNumber(a, b, c) {
    if ("number" == typeof b) {
      // new BigInteger(int,int,RNG)
      if (a < 2) this.bnpFromInt(1);
      else {
        this.bnpFromNumber(a, c);
        if (!this.bnTestBit(a - 1)) // force MSB set
          this.bnpBitwiseTo(BigInteger.ONE.bnShiftLeft(a - 1), op_or, this);
        if (this.bnpIsEven()) this.bnpDAddOffset(1, 0); // force odd
        while (!this.bnIsProbablePrime(b)) {
          this.bnpDAddOffset(2, 0);
          if (this.bnBitLength() > a) this.bnpSubTo(BigInteger.ONE.bnShiftLeft(a - 1), this);
        }
      }
    } else {
      // new BigInteger(int,RNG)
      const x = new Array(),
        t = a & 7;
      x.length = (a >> 3) + 1;
      b.nextBytes(x);
      if (t > 0) x[0] &= ((1 << t) - 1);
      else x[0] = 0;
      this.bnpFromString(x, 256);
    }
  }
  // (public) convert to bigendian byte array
  bnToByteArray() {
    let i = this.t,
      r = new Array();
    r[0] = this.s;
    let p = here.DB - (i * here.DB) % 8,
      d, k = 0;
    if (i-- > 0) {
      if (p < here.DB && (d = this[i] >> p) != (this.s & here.DM) >> p)
        r[k++] = d | (this.s << (here.DB - p));
      while (i >= 0) {
        if (p < 8) {
          d = (this[i] & ((1 << p) - 1)) << (8 - p);
          d |= this[--i] >> (p += here.DB - 8);
        } else {
          d = (this[i] >> (p -= 8)) & 0xff;
          if (p <= 0) {
            p += here.DB;
            --i;
          }
        }
        if ((d & 0x80) != 0) d |= -256;
        if (k == 0 && (this.s & 0x80) != (d & 0x80)) ++k;
        if (k > 0 || d != this.s) r[k++] = d;
      }
    }
    return r;
  }
  bnEquals(a) {
    return this.bnCompareTo(a) == 0
  }
  bnMin(a) {
    return (this.bnCompareTo(a) < 0) ? this : a;
  }
  bnMax(a) {
    return (this.bnCompareTo(a) > 0) ? this : a;
  }
  // r = this op a (bitwise)
  bnpBitwiseTo(a, op, r) {
    let i, f, m = Math.min(a.t, this.t);
    for (i = 0; i < m; ++i) r[i] = op(this[i], a[i]);
    if (a.t < this.t) {
      f = a.s & here.DM;
      for (i = m; i < this.t; ++i) r[i] = op(this[i], f);
      r.t = this.t;
    } else {
      f = this.s & here.DM;
      for (i = m; i < a.t; ++i) r[i] = op(f, a[i]);
      r.t = a.t;
    }
    r.s = op(this.s, a.s);
    r.bnpClamp();
    return r
  }
  bnAnd(a) {
    return this.bnpBitwiseTo(a, op_and, new BigInteger());
  }
  bnOr(a) {
    return this.bnpBitwiseTo(a, op_or, new BigInteger());
  }
  bnXor(a) {
    return this.bnpBitwiseTo(a, op_xor, new BigInteger());
  }
  bnAndNot(a) {
    return this.bnpBitwiseTo(a, op_andnot, new BigInteger());
  }
  // ~this
  bnNot() {
    const r = new BigInteger()
    for (let i = 0; i < this.t; ++i) {
      r[i] = here.DM & ~this[i];
    }
    r.t = this.t;
    r.s = ~this.s;
    return r;
  }
  // (public) this << n
  bnShiftLeft(n) {
    const r = new BigInteger()
    if (n < 0) this.bnpRShiftTo(-n, r);
    else this.bnpLShiftTo(n, r);
    return r;
  }
  // (public) this >> n
  bnShiftRight(n) {
    const r = new BigInteger()
    if (n < 0) this.bnpLShiftTo(-n, r);
    else this.bnpRShiftTo(n, r);
    return r;
  }
  // (public) returns index of lowest 1-bit (or -1 if none)
  bnGetLowestSetBit() {
    for (let i = 0; i < this.t; ++i)
      if (this[i] != 0) return i * here.DB + lbit(this[i]);
    if (this.s < 0) return this.t * here.DB;
    return -1;
  }
  // (public) return number of set bits
  bnBitCount() {
    let r = 0,
      x = this.s & here.DM;
    for (let i = 0; i < this.t; ++i) r += cbit(this[i] ^ x);
    return r;
  }
  // (public) true iff nth bit is set
  bnTestBit(n) {
    const j = Math.floor(n / here.DB);
    if (j >= this.t) return (this.s != 0);
    return ((this[j] & (1 << (n % here.DB))) != 0);
  }
  // (protected) this op (1<<n)
  bnpChangeBit(n, op) {
    const r = BigInteger.ONE.bnShiftLeft(n);
    this.bnpBitwiseTo(r, op, r);
    return r;
  }
  // (public) this | (1<<n)
  bnSetBit(n) {
    return this.bnpChangeBit(n, op_or);
  }
  // (public) this & ~(1<<n)
  bnClearBit(n) {
    return this.bnpChangeBit(n, op_andnot);
  }
  // (public) this ^ (1<<n)
  bnFlipBit(n) {
    return this.bnpChangeBit(n, op_xor);
  }
  // (protected) r = this + a
  bnpAddTo(a, r) {
    let i = 0,
      c = 0,
      m = Math.min(a.t, this.t);
    while (i < m) {
      c += this[i] + a[i];
      r[i++] = c & here.DM;
      c >>= here.DB;
    }
    if (a.t < this.t) {
      c += a.s;
      while (i < this.t) {
        c += this[i];
        r[i++] = c & here.DM;
        c >>= here.DB;
      }
      c += this.s;
    } else {
      c += this.s;
      while (i < a.t) {
        c += a[i];
        r[i++] = c & here.DM;
        c >>= here.DB;
      }
      c += a.s;
    }
    r.s = (c < 0) ? -1 : 0;
    if (c > 0) r[i++] = c;
    else if (c < -1) r[i++] = here.DV + c;
    r.t = i;
    r.bnpClamp();
  }
  // (public) this + a
  bnAdd(a) {
    const r = new BigInteger()
    this.bnpAddTo(a, r);
    return r;
  }
  // (public) this - a
  bnSubtract(a) {
    const r = new BigInteger()
    this.bnpSubTo(a, r);
    return r;
  }
  // (public) this * a
  bnMultiply(a) {
    const r = new BigInteger()
    this.bnpMultiplyTo(a, r);
    return r;
  }
  // (public) this^2
  bnSquare() {
    const r = new BigInteger()
    this.squareTo(r);
    return r;
  }
  // (public) this / a
  bnDivide(a) {
    const r = new BigInteger()
    this.bnpDivRemTo(a, r, null);
    return r;
  }
  // (public) this % a
  bnRemainder(a) {
    const r = new BigInteger()
    this.bnpDivRemTo(a, null, r);
    return r;
  }
  // (public) [this/a,this%a]
  bnDivideAndRemainder(a) {
    const q = new BigInteger();
    const r = new BigInteger();
    this.bnpDivRemTo(a, q, r);
    return new Array(q, r);
  }
  // (protected) this *= n, this >= 0, 1 < n < DV
  bnpDMultiply(n) {
    this[this.t] = this.am(0, n - 1, this, 0, 0, this.t);
    ++this.t;
    this.bnpClamp();
  }
  // (protected) this += n << w words, this >= 0
  bnpDAddOffset(n, w) {
    if (n == 0) return;
    while (this.t <= w) this[this.t++] = 0;
    this[w] += n;
    while (this[w] >= here.DV)
    {
      this[w] -= here.DV;
      if (++w >= this.t) this[this.t++] = 0;
      ++this[w];
    }
  }
  // (public) this^e
  bnPow(e) {
    return this.bnpExp(e, new NullExp());
  }
  // (protected) r = lower n words of "this * a", a.t <= n
  // "this" should be the larger one if appropriate.
  bnpMultiplyLowerTo(a, n, r) {
    let i = Math.min(this.t + a.t, n);
    r.s = 0; // assumes a,this >= 0
    r.t = i;
    while (i > 0) r[--i] = 0;
    let j;
    for (j = r.t - this.t; i < j; ++i) r[i + this.t] = this.am(0, a[i], r, i, 0, this.t);
    for (j = Math.min(a.t, n); i < j; ++i) this.am(0, a[i], r, i, 0, n - i);
    r.bnpClamp();
  }
  // (protected) r = "this * a" without lower n words, n > 0
  // "this" should be the larger one if appropriate.
  bnpMultiplyUpperTo(a, n, r) {
    --n;
    let i = r.t = this.t + a.t - n;
    r.s = 0; // assumes a,this >= 0
    while (--i >= 0) r[i] = 0;
    for (i = Math.max(n - this.t, 0); i < a.t; ++i)
      r[this.t + i - n] = this.am(n - i, a[i], r, 0, 0, this.t + i - n);
    r.bnpClamp();
    r.bnpDRShiftTo(1, r);
  }
  // (public) this^e % m (HAC 14.85)
  bnModPow(e, m) {
    let i = e.bnBitLength(),
      k, r = nbv(1),
      z;
    if (i <= 0) return r;
    else if (i < 18) k = 1;
    else if (i < 48) k = 3;
    else if (i < 144) k = 4;
    else if (i < 768) k = 5;
    else k = 6;
    if (i < 8)
      z = new Classic(m);
    else if (m.bnpIsEven())
      z = new Barrett(m);
    else
      z = new Montgomery(m);
    // precomputation
    let g = new Array(),
      n = 3,
      k1 = k - 1,
      km = (1 << k) - 1;
    g[1] = z.convert(this);
    if (k > 1) {
      let g2 = new BigInteger()
      z.sqrTo(g[1], g2);
      while (n <= km) {
        g[n] = new BigInteger()
        z.mulTo(g2, g[n - 2], g[n]);
        n += 2;
      }
    }
    let j = e.t - 1,
      w, is1 = true,
      r2 = new BigInteger(),
      t;
    i = nbits(e[j]) - 1;
    while (j >= 0) {
      if (i >= k1) w = (e[j] >> (i - k1)) & km;
      else {
        w = (e[j] & ((1 << (i + 1)) - 1)) << (k1 - i);
        if (j > 0) w |= e[j - 1] >> (here.DB + i - k1);
      }
      n = k;
      while ((w & 1) == 0) {
        w >>= 1;
        --n;
      }
      if ((i -= n) < 0) {
        i += here.DB;
        --j;
      }
      if (is1) {
        // ret == 1, don't bother squaring or multiplying it
        g[w].bnpCopyTo(r);
        is1 = false;
      } else {
        while (n > 1) {
          z.sqrTo(r, r2);
          z.sqrTo(r2, r);
          n -= 2;
        }
        if (n > 0) z.sqrTo(r, r2);
        else {
          t = r;
          r = r2;
          r2 = t;
        }
        z.mulTo(r2, g[w], r);
      }
      while (j >= 0 && (e[j] & (1 << i)) == 0) {
        z.sqrTo(r, r2);
        t = r;
        r = r2;
        r2 = t;
        if (--i < 0)
        {
          i = here.DB - 1;
          --j;
        }
      }
    }
    return z.revert(r);
  }
  // (public) gcd(this,a) (HAC 14.54)
  bnGCD(a) {
    let x = (this.s < 0) ? this.bnNegate() : this.bnClone();
    let y = (a.s < 0) ? a.bnNegate() : a.bnClone();
    if (x.bnCompareTo(y) < 0) {
      const t = x;
      x = y;
      y = t;
    }
    let i = x.bnGetLowestSetBit(),
      g = y.bnGetLowestSetBit();
    if (g < 0) return x;
    if (i < g) g = i;
    if (g > 0) {
      x.bnpRShiftTo(g, x);
      y.bnpRShiftTo(g, y);
    }
    while (x.bnSigNum() > 0) {
      if ((i = x.bnGetLowestSetBit()) > 0) x.bnpRShiftTo(i, x);
      if ((i = y.bnGetLowestSetBit()) > 0) y.bnpRShiftTo(i, y);
      if (x.bnCompareTo(y) >= 0)
      {
        x.bnpSubTo(y, x);
        x.bnpRShiftTo(1, x);
      }
      else
      {
        y.bnpSubTo(x, y);
        y.bnpRShiftTo(1, y);
      }
    }
    if (g > 0) y.bnpLShiftTo(g, y);
    return y;
  }
  // (protected) this % n, n < 2^26
  bnpModInt(n) {
    if (n <= 0) return 0;
    const d = here.DV % n
    let r = (this.s < 0) ? n - 1 : 0;
    if (this.t > 0)
      if (d == 0) r = this[0] % n;
      else
        for (let i = this.t - 1; i >= 0; --i) r = (d * r + this[i]) % n;
    return r;
  }
  // (public) 1/this % m (HAC 14.61)
  bnModInverse(m) {
    let ac = m.bnpIsEven();
    if ((this.bnpIsEven() && ac) || m.bnSigNum() == 0) return BigInteger.ZERO;
    let u = m.clone(),
      v = this.clone();
    let a = nbv(1),
      b = nbv(0),
      c = nbv(0),
      d = nbv(1);
    while (u.bnSigNum() != 0) {
      while (u.bnpIsEven()) {
        u.bnpRShiftTo(1, u);
        if (ac) {
          if (!a.bnpIsEven() || !b.bnpIsEven()) {
            a.bnpAddTo(this, a);
            b.bnpSubTo(m, b);
          }
          a.bnpRShiftTo(1, a);
        }
        else if (!b.bnpIsEven()) b.bnpSubTo(m, b);
        b.bnpRShiftTo(1, b);
      }
      while (v.bnpIsEven()) {
        v.bnpRShiftTo(1, v);
        if (ac) {
          if (!c.bnpIsEven() || !d.bnpIsEven()) {
            c.bnpAddTo(this, c);
            d.bnpSubTo(m, d);
          }
          c.bnpRShiftTo(1, c);
        }
        else if (!d.bnpIsEven()) d.bnpSubTo(m, d);
        d.bnpRShiftTo(1, d);
      }
      if (u.bnCompareTo(v) >= 0) {
        u.bnpSubTo(v, u);
        if (ac) a.bnpSubTo(c, a);
        b.bnpSubTo(d, b);
      } else {
        v.bnpSubTo(u, v);
        if (ac) c.bnpSubTo(a, c);
        d.bnpSubTo(b, d);
      }
    }
    if (v.bnCompareTo(BigInteger.ONE) != 0) return BigInteger.ZERO;
    if (d.bnCompareTo(m) >= 0) return d.bnSubtract(m);
    if (d.bnSigNum() < 0) d.bnpAddTo(m, d);
    else return d;
    if (d.bnSigNum() < 0) return d.bnAdd(m);
    else return d;
  }
  // (public) test primality with certainty >= 1-.5^t
  bnIsProbablePrime(t) {
    let i, x = this.bnAbs();
    if (x.t == 1 && x[0] <= lowprimes[lowprimes.length - 1]) {
      for (i = 0; i < lowprimes.length; ++i)
        if (x[0] == lowprimes[i]) return true;
      return false;
    }
    if (x.bnpIsEven()) return false;
    i = 1;
    while (i < lowprimes.length) {
      let m = lowprimes[i],
        j = i + 1;
      while (j < lowprimes.length && m < lplim) m *= lowprimes[j++];
      m = x.bnpModInt(m);
      while (i < j)
        if (m % lowprimes[i++] == 0) return false;
    }
    return x.bnpMillerRabin(t);
  }
  // (protected) true if probably prime (HAC 4.24, Miller-Rabin)
  bnpMillerRabin(t) {
    let n1 = this.bnSubtract(BigInteger.ONE);
    let k = n1.bnGetLowestSetBit();
    if (k <= 0) return false;
    let r = n1.bnShiftRight(k);
    t = (t + 1) >> 1;
    if (t > lowprimes.length) t = lowprimes.length;
    let a = new BigInteger()
    for (let i = 0; i < t; ++i) {
      //Pick bases at random, instead of starting at 2
      a.bnpFromInt(lowprimes[Math.floor(Math.random() * lowprimes.length)]);
      let y = a.bnModPow(r, this);
      if (y.bnCompareTo(BigInteger.ONE) != 0 && y.bnCompareTo(n1) != 0) {
        let j = 1;
        while (j++ < k && y.bnCompareTo(n1) != 0) {
          y = y.bnModPowInt(2, this);
          if (y.bnCompareTo(BigInteger.ONE) == 0) return false;
        }
        if (y.bnCompareTo(n1) != 0) return false;
      }
    }
    return true;
  }

  /**
   * big number from string
   * @param {string} s
   * @param {number} b
   */
  bnpFromString(s, b) {
    let k;
    if (b == 16) k = 4;
    else if (b == 8) k = 3;
    else if (b == 256) k = 8; // byte array
    else if (b == 2) k = 1;
    else if (b == 32) k = 5;
    else if (b == 4) k = 2;
    else {
      this.bnpFromRadix(s, b);
      return;
    }
    this.t = 0;
    this.s = 0;
    let i = s.length,
      mi = false,
      sh = 0;
    while (--i >= 0) {
      let x = (k == 8) ? s[i] & 0xff : intAt(s, i);
      if (x < 0) {
        if (s.charAt(i) == "-") mi = true;
        continue;
      }
      mi = false;
      if (sh == 0)
        this[this.t++] = x;
      else if (sh + k > here.DB) {
        this[this.t - 1] |= (x & ((1 << (here.DB - sh)) - 1)) << sh;
        this[this.t++] = (x >> (here.DB - sh));
      }
      else
        this[this.t - 1] |= x << sh;
      sh += k;
      if (sh >= here.DB) sh -= here.DB;
    }
    if (k == 8 && (s[0] & 0x80) != 0) {
      this.s = -1;
      if (sh > 0) this[this.t - 1] |= ((1 << (here.DB - sh)) - 1) << sh;
    }
    this.bnpClamp();
    if (mi) BigInteger.ZERO.bnpSubTo(this, this);
  }
}

class Classic extends BigInteger {
  constructor(m) {
    super()
    this.m = m
  }
  convert(x) {
    if (x.s < 0 || x.bnCompareTo(this.m) >= 0) return x.bnMod(this.m);
    else return x;
  }
  revert(x) {
    return x;
  }
  reduce(x) {
    x.bnpDivRemTo(this.m, null, x);
  }
  mulTo(x, y, r) {
    x.bnpMultiplyTo(y, r);
    this.reduce(r);
  }
  sqrTo(x, r) {
    x.bnpSquareTo(r);
    this.reduce(r);
  }
}

class Montgomery extends BigInteger {
  constructor(m) {
    super()
    this.m = m;
    this.mp = m.bnpInvDigit();
    this.mpl = this.mp & 0x7fff;
    this.mph = this.mp >> 15;
    this.um = (1 << (here.DB - 15)) - 1;
    this.mt2 = 2 * m.t;
  }
  // xR mod m
  convert(x) {
    const r = new BigInteger()
    x.abs().bnpDLShiftTo(this.m.t, r);
    r.bnpDivRemTo(this.m, null, r);
    if (x.s < 0 && r.bnCompareTo(BigInteger.ZERO) > 0) this.m.bnpSubTo(r, r);
    return r;
  }
  // x/R mod m
  revert(x) {
    const r = new BigInteger()
    x.bnpCopyTo(r);
    this.reduce(r);
    return r;
  }
  // x = x/R mod m (HAC 14.32)
  reduce(x) {
    while (x.t <= this.mt2) // pad x so am has enough room later
      x[x.t++] = 0;
    for (let i = 0; i < this.m.t; ++i) {
      // faster way of calculating u0 = x[i]*mp mod DV
      let j = x[i] & 0x7fff;
      const u0 = (j * this.mpl + (((j * this.mph + (x[i] >> 15) * this.mpl) & this.um) << 15)) & here.DM;
      // use am to combine the multiply-shift-add into one call
      j = i + this.m.t;
      x[j] += this.m.am(0, u0, x, i, 0, this.m.t);
      // propagate carry
      while (x[j] >= here.DV)
      {
        x[j] -= here.DV;
        x[++j]++;
      }
    }
    x.bnpClamp();
    x.bnpDRShiftTo(this.m.t, x);
    if (x.bnCompareTo(this.m) >= 0) x.bnpSubTo(this.m, x);
  }
  // r = "xy/R mod m"; x,y != r
  mulTo(x, y, r) {
    x.bnpMultiplyTo(y, r);
    this.reduce(r);
  }
  // r = "x^2/R mod m"; x != r
  sqrTo(x, r) {
    x.bnpSquareTo(r);
    this.reduce(r);
  }
}

class NullExp {
  nNop(x) {
    return x;
  }
  convert(x) {
    return x;
  }
  // x/R mod m
  revert(x) {
    return x;
  }
  // r = "xy/R mod m"; x,y != r
  mulTo(x, y, r) {
    x.bnpMultiplyTo(y, r);
  }
  // r = "x^2/R mod m"; x != r
  sqrTo(x, r) {
    x.bnpSquareTo(r);
  }
}
// Barrett modular reduction
class Barrett {
  constructor(m) {
    // setup Barrett
    this.r2 = new BigInteger()
    this.q3 = new BigInteger()
    BigInteger.ONE.bnpDLShiftTo(2 * m.t, this.r2);
    this.mu = this.r2.bnDivide(m);
    this.m = m;
  }
  convert(x) {
    if (x.s < 0 || x.t > 2 * this.m.t) return x.bnMod(this.m);
    else if (x.bnCompareTo(this.m) < 0) return x;
    else {
      const r = new BigInteger()
      x.bnpCopyTo(r);
      this.reduce(r);
      return r;
    }
  }
  revert(x) {
    return x
  }
  // x = x mod m (HAC 14.42)
  reduce(x) {
    x.bnpDRShiftTo(this.m.t - 1, this.r2);
    if (x.t > this.m.t + 1)
    {
      x.t = this.m.t + 1;
      x.bnpClamp();
    }
    this.mu.bnpMultiplyUpperTo(this.r2, this.m.t + 1, this.q3);
    this.m.bnpMultiplyLowerTo(this.q3, this.m.t + 1, this.r2);
    while (x.bnCompareTo(this.r2) < 0) x.bnpDAddOffset(1, this.m.t + 1);
    x.bnpSubTo(this.r2, x);
    while (x.bnCompareTo(this.m) >= 0) x.bnpSubTo(this.m, x);
  }
  // r = x^2 mod m; x != r
  sqrTo(x, r) {
    x.bnpSquareTo(r);
    this.reduce(r);
  }
  // r = x*y mod m; x,y != r
  mulTo(x, y, r) {
    x.bnpMultiplyTo(y, r);
    this.reduce(r);
  }
}

export default class Int128 extends BigInteger {
  IsNegative() {
    return this.bnCompareTo(Int128.ZERO) == -1
  }
  op_Equality(val1, val2) {
    return val1.bnCompareTo(val2) == 0
  }
  op_Inequality(val1, val2) {
    return val1.bnCompareTo(val2) != 0
  }
  op_GreaterThan(val1, val2) {
    return val1.bnCompareTo(val2) > 0
  }
  op_LessThan(val1, val2) {
    return val1.bnCompareTo(val2) < 0
  }
  op_Addition(lhs, rhs) {
    return new Int128(lhs, undefined, undefined).bnAdd(new Int128(rhs, undefined, undefined));
  }

  op_Subtraction(lhs, rhs) {
    return new Int128(lhs, undefined, undefined).bnSubtract(new Int128(rhs, undefined, undefined));
  }
  Int128Mul(lhs, rhs) {
    return new Int128(lhs, undefined, undefined).bnMultiply(new Int128(rhs, undefined, undefined));
  }
  op_Division(lhs, rhs) {
    return lhs.bnDivide(rhs);
  }
  ToDouble() {
    return parseFloat(this.bnToString()); // This could be something faster
  }
}

function nbv(value) {
  return new BigInteger().bnpFromInt(value)
}
BigInteger.ZERO = nbv(0)
BigInteger.ONE = nbv(1)
if (j_lm && (navigator_appName == "Microsoft Internet Explorer")) {
  BigInteger.prototype.am = am2;
  dbits = 30;
} else if (j_lm && (navigator_appName != "Netscape")) {
  BigInteger.prototype.am = am1;
  dbits = 26;
} else { // Mozilla/Netscape seems to prefer am3
  BigInteger.prototype.am = am3;
  dbits = 28;
}
